<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showRoleDialog" @close="closeFn">
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="checkbox">
      <!-- 选项 -->
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="yesFn">确定</el-button>
      <el-button size="small">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserPhotoAPI } from '@/api/user'
import { getJurisdictionMessageAPI, assignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 储存角色列表
      roleList: [],
      checkbox: [],
      userId: ''
    }
  },
  created() {
    this.getRoleListFn()
  },
  methods: {
    // 回填已拥有的角色
    async getUserRoleListFn(id) {
      console.log(id)
      this.userId = id
      const { roleIds } = await getUserPhotoAPI(id)
      console.log('这', roleIds)
      // console.log('希望', await getJurisdictionMessageAPI('604e2b19488be61b90b6877a'))
      // 保存角色id
      const arry = []
      roleIds.forEach(async item => {
        // 获取角色详情
        console.log(item)
        const res = await getJurisdictionMessageAPI(item)
        // arry.push(res)
        arry.push(res.id)// 有用的
        console.log('这是arry数组', arry)
      })
      console.log('需要后执行')
      // console.log(arry)
      // console.log(roleIds)
      // console.log('这是arry数组', arry)==========================================================================

      // console.log('要获取数组中的对象id', arry[0].id)
      // ==================================
      // const checkboxArry = []
      // arry.forEach((item) => {
      //   console.log('item', item)
      //   checkboxArry.push(item.id)
      // })
      // this.checkbox = checkboxArry
      // console.log(checkboxArry)
      // 角色id双向绑定给多选框
      this.checkbox = arry
      console.log('checkbox后面的', arry)
    },
    // 获取总角色列表
    async getRoleListFn() {
      const { rows } = await getRoleList({ page: 1, pagesize: 1000 })
      this.roleList = rows
      console.table(rows)
    },
    // 关闭窗口事件
    closeFn() {
      this.$emit('update:showRoleDialog', false)
    },
    // 确认分配按钮
    async yesFn() {
      await assignRoles({ id: this.userId, roleIds: this.checkbox })
      this.$message.success('分配成功')
      this.closeFn()
    }
  }
}
</script>
